#include "ImageIOWizard.h"
#include "GuidedNativeImageIO.h"
#include "ui_FilePage.h"
#include "ui_HeaderPage.h"
#include "ui_ACTHeaderPage.h"
#include "ui_ACT_ROIPage.h"

#include <QtGui>
#include <QMessageBox>
#include <QErrorMessage>
#include <QStringList>
#include <QDir>
#include <QFileInfo>
#include <QTableWidget>
#include <QTableWidgetItem>
#include <QAbstractItemView>

#include <cstdlib>

ImageIOWizard::ImageIOWizard(QWidget *parent):QWizard(parent)
{
    m_GuidedIO = new GuidedNativeImageIO;

    setWindowTitle("Wizard for loading a greyscale image");
    setPage(Page_File, new FilePage(m_GuidedIO));
    setPage(Page_DICOM, new HeaderPage(m_GuidedIO));
    setPage(Page_ACT, new ACTHeaderPage(m_GuidedIO));
    setPage(PageROI_ACT, new ACT_ROIPage(m_GuidedIO));
    setFocusPolicy(Qt::StrongFocus);
}

ImageIOWizard::~ImageIOWizard()
{
    delete m_GuidedIO;
}


FilePage::FilePage(GuidedNativeImageIO *guidedIO, QWidget *parent):QWizardPage(parent), ui(new Ui::FilePage)
{
 //   if ((!m_FPGuidedIO)||(!m_FPOpenFiles))
 //           exit(1);
    m_FPGuidedIO = guidedIO;

    ui->setupUi(this);
    this->ui->DICOMBox->hide();
    this->ui->uidBox->setMaximumWidth(80);
    this->layout()->setSizeConstraint(QLayout::SetFixedSize);
    setTitle(tr("Open"));
    setPixmap(QWizard::WatermarkPixmap, QPixmap(":/ImageIOWizard/Flower.png"));

    registerField("uid*", this->ui->uidBox, "currentText", SIGNAL(currentIndexChanged()));
 }

void
FilePage::BrowsePushed()
{
    QStringList openFiles;
    QDir directory;
    QFileDialog dialog(this, "Select one or more files to open","~/");
    dialog.setFileMode(QFileDialog::ExistingFiles);

    if (dialog.exec())
    {
        openFiles = dialog.selectedFiles();
        directory = dialog.directory();
    }

    QString fileText = openFiles[0];
    m_FileFormat = m_FPGuidedIO->DetermineFileFormatFromFile(openFiles, directory);

    if (m_FileFormat == GuidedNativeImageIO::FORMAT_DICOM)
    {
        registerField("pathToDICOMFile*", this->ui->DICOMlineEdit);
        if (openFiles.size() > 1)
        {
            const QStringList uids = m_FPGuidedIO->GetDICOMSeriesUID(directory);
            if (uids.size() > 0)
            {
                this->ui->sequenceButton->setChecked(true);
                this->ui->uidBox->setCurrentIndex(0);
                this->ui->uidBox->addItem("No UID");
                for (int i = 0; i < uids.size(); i++)
                {
                    this->ui->uidBox->addItem(uids[i]);
                }
            }
            else
                this->ui->directoryButton->setChecked(true);
            this->ui->DICOMBox->show();
        }
    }
    if (m_FileFormat == GuidedNativeImageIO::FORMAT_ACT)
    {
        registerField("pathToACTFile*", this->ui->ACTlineEdit);
        if (openFiles.size() > 1)
        {
            QStringList uids = m_FPGuidedIO->GetACTSeriesUID(directory);
            if (uids.size() > 0)
            {
                this->ui->sequenceButton->setChecked(true);
                this->ui->uidBox->setCurrentIndex(0);
                this->ui->uidBox->addItem("No UID");
                for (int i = 0; i < uids.size(); i++)
                {
                    this->ui->uidBox->addItem(uids[i]);
                }
            }
            else
                this->ui->directoryButton->setChecked(true);
            this->ui->DICOMBox->show();
        }
    }

    if (openFiles.size() > 1)
    {
        QFileInfo onlyName(openFiles[openFiles.size()-1]);
        fileText.append(" ... "+onlyName.fileName());

    }

    if (m_FileFormat == GuidedNativeImageIO::FORMAT_DICOM)
    {
        this->ui->DICOMlineEdit->setText(fileText);
        this->ui->DICOMlineEdit->setFocus();
    }
    else if (m_FileFormat == GuidedNativeImageIO::FORMAT_ACT)
    {
        this->ui->ACTlineEdit->setText(fileText);
        this->ui->ACTlineEdit->setFocus();
    }
}

int
FilePage::nextId() const
{
    if (m_FileFormat == GuidedNativeImageIO::FORMAT_DICOM)
        return ImageIOWizard::Page_DICOM;
    else
        return ImageIOWizard::Page_ACT;
}

HeaderPage::HeaderPage(GuidedNativeImageIO *guidedIO, QWidget *parent):QWizardPage(parent), ui(new Ui::HeaderPage)
{
   // if ((!m_HPGuidedIO)||(!m_HPOpenFiles))
   //         exit(2);
    m_HPGuidedIO = guidedIO;

    ui->setupUi(this);
    setTitle(tr("DICOM Image Summary"));
    this->ui->headerTable->setEditTriggers(QAbstractItemView::NoEditTriggers);
}

void
HeaderPage::initializePage()
{
    QString fileString = field("pathToDICOMFile").toString();
    this->ui->fileNameEdit->setText(fileString);

    m_HPGuidedIO->ReadNativeImage(field("uid").toString());
    m_ImageProperties = m_HPGuidedIO->GetImageProperties();

    this->ui->DimEdit_1->setText(QString::number(m_ImageProperties->dimensions[0]));
    this->ui->DimEdit_2->setText(QString::number(m_ImageProperties->dimensions[1]));
    this->ui->DimEdit_3->setText(QString::number(m_ImageProperties->dimensions[2]));

    this->ui->SpEdit_1->setText(QString::number(m_ImageProperties->spacing[0]));
    this->ui->SpEdit_2->setText(QString::number(m_ImageProperties->spacing[1]));
    this->ui->SpEdit_3->setText(QString::number(m_ImageProperties->spacing[2]));

    this->ui->OrEdit_1->setText(QString::number(m_ImageProperties->imagePosition[0]));
    this->ui->OrEdit_2->setText(QString::number(m_ImageProperties->imagePosition[1]));
    this->ui->OrEdit_3->setText(QString::number(m_ImageProperties->imagePosition[2]));

    this->ui->OrientationEdit->setText(m_ImageProperties->lps);
    this->ui->SizeEdit->setText(QString::number(m_ImageProperties->sizeInKBytes));
    this->ui->DTypeEdit->setText(m_ImageProperties->pixelTypeString);
    this->ui->BOrderEdit->setText(m_ImageProperties->endian);

    this->ui->headerTable->setRowCount((int)m_ImageProperties->labels.size());
    int index = 0;
    QMap<QString, QString>::const_iterator iterator;
    for(iterator = m_ImageProperties->labels.constBegin(); iterator != m_ImageProperties->labels.constEnd(); ++iterator)
    {
        QTableWidgetItem *keyItem = new QTableWidgetItem(iterator.key());
        QTableWidgetItem *valueItem = new QTableWidgetItem(iterator.value());
        this->ui->headerTable->setItem(index, 0, keyItem);
        this->ui->headerTable->setItem(index, 1, valueItem);
        index++;
    }
}

ACTHeaderPage::ACTHeaderPage(GuidedNativeImageIO *guidedIO, QWidget *parent):QWizardPage(parent), ui(new Ui::ACTHeaderPage)
{
    m_ACTHPGuidedIO = guidedIO;

    ui->setupUi(this);
    setTitle(tr("ACT Image Summary"));
 }

void
ACTHeaderPage::initializePage()
{
    QString fileString = field("pathToACTFile").toString();
    this->ui->fileNameEdit->setText(fileString);

    m_ACTHPGuidedIO->ReadNativeImage(field("uid").toString());
    m_ImageProperties = m_ACTHPGuidedIO->GetImageProperties();

    this->ui->DimEdit_1->setText(QString::number(m_ImageProperties->dimensions[0]));
    this->ui->DimEdit_2->setText(QString::number(m_ImageProperties->dimensions[1]));
    this->ui->DimEdit_3->setText(QString::number(m_ImageProperties->dimensions[2]));

    this->ui->SpEdit_1->setText(QString::number(m_ImageProperties->spacing[0]));
    this->ui->SpEdit_2->setText(QString::number(m_ImageProperties->spacing[1]));
    this->ui->SpEdit_3->setText(QString::number(m_ImageProperties->spacing[2]));

    this->ui->OrEdit_1->setText(QString::number(m_ImageProperties->imagePosition[0]));
    this->ui->OrEdit_2->setText(QString::number(m_ImageProperties->imagePosition[1]));
    this->ui->OrEdit_3->setText(QString::number(m_ImageProperties->imagePosition[2]));

    this->ui->OrientationEdit->setText(m_ImageProperties->lps);
    this->ui->DTypeEdit->setText(m_ImageProperties->pixelTypeString);
    this->ui->BOrderEdit->setText(m_ImageProperties->endian);

    this->ui->ModEdit->setText(m_ImageProperties->modality);
    this->ui->DBaseEdit->setText(m_ImageProperties->dataBase);
    this->ui->PatientEdit->setText(m_ImageProperties->patientNumber);
}

int
ACTHeaderPage::nextId() const
{
    return ImageIOWizard::PageROI_ACT;
}

ACT_ROIPage::ACT_ROIPage(GuidedNativeImageIO *guidedIO, QWidget *parent):QWizardPage(parent), ui(new Ui::ACT_ROIPage)
{
    m_ACTROIGuidedIO = guidedIO;

    ui->setupUi(this);
    setTitle(tr("ACT Image ROI"));
}

void
ACT_ROIPage::BrowsePushed()
{
    QStringList openFile;
    QDir directory;
    QFileDialog dialog(this, "Select one or more files to open", "~/", tr("ROI Information Files (*.cst)"));
    dialog.setFileMode(QFileDialog::ExistingFile);

    if (dialog.exec())
        openFile = dialog.selectedFiles();

    m_ACTROIGuidedIO->ReadACTROIFile(openFile[0]);
}

